﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using LibraryO2S;

public partial class Administrator_News : System.Web.UI.Page
{


    BizNews news = new BizNews();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadToCombobox();
        }
    }


    private void LoadData()
    {
        try
        {
            CollectionPager1.DataSource = news.NewsSelectAll().DefaultView;
            CollectionPager1.DataBind();
            CollectionPager1.PageSize = 5;
            CollectionPager1.BindToControl = rptData;
            rptData.DataSource = CollectionPager1.DataSourcePaged;
            rptData.DataBind();
            if (rptData.Items.Count == 0)
            {
                divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
            }
        }
        catch
        {
            divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {

            string del_list = hdfCheck.Value;
            string[] sp_list = del_list.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < sp_list.Length; i++)
            {
                
                news.tblNewsDelete(int.Parse(sp_list[i]));
            }
            LoadData();
        }
        catch (Exception ex)
        {

            divMessage.InnerHtml = O2S_Message.Error("Cảnh Báo", "Phát hiện lỗi! " + ex.Message);
        }

    }
    protected void btnAdnew1_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewsAdd.aspx");
    }
    public void LoadToCombobox()
    {

        BizModule module = new BizModule();
        ddlModule.DataSource = module.SelectAll();
        ddlModule.DataTextField = "DESCRIPTION";
        ddlModule.DataValueField = "MODULEID";
        ddlModule.DataBind();
        ddlModule.Items.Add(new ListItem("Xem tất cả", "0"));
        ddlModule.SelectedValue = "0";

    }
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        divMessage.InnerHtml = "";
        if (ddlModule.SelectedValue == "0")
        {
            LoadData();
        }
        else
        {
            CollectionPager1.DataSource = news.SelectByModuleID(int.Parse(ddlModule.SelectedValue)).DefaultView;
            CollectionPager1.DataBind();
            CollectionPager1.PageSize = int.MaxValue ;
            CollectionPager1.BindToControl = rptData;
            rptData.DataSource = CollectionPager1.DataSourcePaged;
            rptData.DataBind();
            if (rptData.Items.Count == 0)
            {
                divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
            }
        }
    }
    protected void btnDelete2_Click(object sender, EventArgs e)
    {
        btnDelete_Click(sender, e); 

    }
}